package com.secsys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.secsys.dto.StoPriceearningDTO;
import com.secsys.dto.StoStockDTO;
import com.secsys.model.TbStoPriceearning;
import com.secsys.model.TbStoStock;
import com.secsys.service.StoPriceearningService;
import com.secsys.service.StoStockService;

public class StoPriceearningServiceImpl extends
		ServiceImpl<TbStoPriceearning, Integer> implements StoPriceearningService {

	private StoStockService stoStockService;
	
	@Override
	public List<StoPriceearningDTO> queryStoPriceearningByStockCode(String stockCode) {
		TbStoStock tbStoStock=stoStockService.findByProperty("from TbStoStock where stockCode=?", stockCode).get(0);
		return queryStoPriceearningByTbStoStock(tbStoStock);
	}

	@Override
	public List<StoPriceearningDTO> queryStoPriceearningByTbStoStock(
			TbStoStock tbStoStock) {
		String hql="from TbStoPriceearning as tsp where tsp.tbStoStock=?";
		return foreachGetDtoFromPojo(this.findByProperty(hql, tbStoStock)); 
	}

	@Override
	public List<StoPriceearningDTO> queryStoPriceearningByYear(String year) {
		String hql="from TbStoPriceearning as tsp where tsp.fieldYear=?";
		return foreachGetDtoFromPojo(this.findByProperty(hql, year)); 
	}

	@Override
	public List<StoPriceearningDTO> queryStoPriceearningByStockCodeAndYear(
			int stockCode, String year) {
		TbStoStock tbStoStock=stoStockService.findByProperty("from TbStoStock where stockCode=?", String.valueOf(stockCode)).get(0);
		return queryStoPriceearningByStockCodeAndYear(tbStoStock,year);
	}

	@Override
	public List<StoPriceearningDTO> queryStoPriceearningByStockCodeAndYear(
			TbStoStock tbStoStock, String year) {
		String hql="from TbStoPriceearning as tsp where tsp.tbStoStock=? and tss.fieldYear=?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{tbStoStock,year})); 
	}
	
	@Override
	public List<StoPriceearningDTO> queryStoPriceearningByYearRange(
			String startYear, String endYear) {
		String hql="from TbStoPriceearning as tsp where tsp.fieldYear between ? and ?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{startYear,endYear})); 
	}
	
	@Override
	public int countStoPriceearningBySearch(String startYear,String endYear,double num,int updown){
		String hql=null;
		if (updown==1) {
			hql="from TbStoPriceearning as tsp2,TbStoStock as tss,TbStoPriceearning as tsp1 where tss.id=tsp1.tbStoStock and " +
			" tsp1.tbStoStock=tsp2.tbStoStock and  tsp1.fieldYear=? and tsp2.fieldYear=? and tsp2.wholeYear> tsp1.wholeYear and " +
			" tsp2.wholeYear/tsp1.wholeYear >?";
		}else {
			hql="from TbStoPriceearning as tsp2,TbStoStock as tss,TbStoPriceearning as tsp1 where tss.id=tsp1.tbStoStock and " +
			" tsp1.tbStoStock=tsp2.tbStoStock and  tsp1.fieldYear=? and tsp2.fieldYear=? and tsp2.wholeYear< tsp1.wholeYear and " +
			" tsp1.wholeYear/tsp2.wholeYear >?";
		}
		num=num/100+1;
		List<Object> list=this.dao.findObject(hql, new Object[]{startYear,endYear,num});
		List<StoPriceearningDTO> stoPriceearningDTOList=new ArrayList<StoPriceearningDTO>();
		for (Object object : list) {
			StoPriceearningDTO stoPriceearningDTO=getDtoFromPojo((TbStoPriceearning)((Object[])object)[0]); 
			stoPriceearningDTOList.add(stoPriceearningDTO);
		}
		return list.size();
	}
	
	public HashMap<String, List> queryStoPriceearningBySearch(String startYear,String endYear,double num,int updown,int offset,int count){
		String hql=null;
		if (updown==1) {
			hql="from TbStoPriceearning as tsp2,TbStoStock as tss,TbStoPriceearning as tsp1 where tss.id=tsp1.tbStoStock and " +
			" tsp1.tbStoStock=tsp2.tbStoStock and tss.id=tsp1.tbStoStock and tsp1.fieldYear=? and tsp2.fieldYear=? and tsp2.wholeYear> tsp1.wholeYear and " +
			" tsp2.wholeYear/tsp1.wholeYear >? order by tsp2.wholeYear/tsp1.wholeYear desc";
		}else {
			hql="from TbStoPriceearning as tsp2,TbStoStock as tss,TbStoPriceearning as tsp1 where tss.id=tsp1.tbStoStock and " +
			" tsp1.tbStoStock=tsp2.tbStoStock and tss.id=tsp1.tbStoStock and tsp1.fieldYear=? and tsp2.fieldYear=? and tsp2.wholeYear< tsp1.wholeYear and " +
			" tsp1.wholeYear/tsp2.wholeYear >? order by tsp1.wholeYear/tsp2.wholeYear desc";
		}
		num=num/100+1;
		List<Object> list=this.dao.findObject(hql, new Object[]{startYear,endYear,num},offset,count);
		List<StoPriceearningDTO> stoPriceearningDTOList=new ArrayList<StoPriceearningDTO>();
		List<StoPriceearningDTO> oldYearStoPriceearningDTOList=new ArrayList<StoPriceearningDTO>();
		List<StoStockDTO> stockDTOList=new ArrayList<StoStockDTO>();
		for (Object object : list) {
			StoPriceearningDTO stoPriceearningDTO=getDtoFromPojo((TbStoPriceearning)((Object[])object)[0]); 
			System.out.println(stoPriceearningDTO.getId()+"-in-"+stoPriceearningDTO.getFieldYear()); 
			stoPriceearningDTOList.add(stoPriceearningDTO);
			StoStockDTO stockDTO=stoStockService.getDtoFromPojo((TbStoStock)((Object[])object)[1]);
			stockDTOList.add(stockDTO);
			StoPriceearningDTO oldYearStoPriceearningDTO=getDtoFromPojo((TbStoPriceearning)((Object[])object)[2]); 
			oldYearStoPriceearningDTOList.add(oldYearStoPriceearningDTO);
		}
		HashMap<String, List> map=new HashMap<String, List>(); 
		map.put("stoPriceearningDTOList", stoPriceearningDTOList);
		map.put("stockDTOList", stockDTOList); 
		map.put("oldYearStoPriceearningDTOList", oldYearStoPriceearningDTOList); 
		return map; 
	}
	
	/**
	 * 批量地将数据库持久化对象POJO的tbStoPriceearning转换为领域对象DTO的StoPriceearningDTO
	 * @param 	List<TbStoPriceearning>
	 * 			数据库持久化对象POJO的List<TbStoPriceearning>
	 * @return	List<StoPriceearningDTO>
	 * 			领域对象DTO的List<StoPriceearningDTO>
	 */
	private List<StoPriceearningDTO> foreachGetDtoFromPojo(List<TbStoPriceearning> tbStoPriceearningList){
		List<StoPriceearningDTO> stoPriceearningDTOList=new ArrayList<StoPriceearningDTO>();
		for (TbStoPriceearning tbStoPriceearning : tbStoPriceearningList) {
			stoPriceearningDTOList.add(getDtoFromPojo(tbStoPriceearning));
		}
		return stoPriceearningDTOList;
	}

	/**
	 * 将数据库持久化对象POJO的tbStoPriceearning转换为领域对象DTO的StoPriceearningDTO
	 * @param 	tbStoPriceearning
	 * 			数据库持久化对象POJO的tbStoPriceearning
	 * @return	StoPriceearningDTO
	 * 			领域对象DTO的StoPriceearningDTO
	 */
	private StoPriceearningDTO getDtoFromPojo(TbStoPriceearning tbStoPriceearning){
		StoPriceearningDTO stoPriceearningDTO=new StoPriceearningDTO(
				tbStoPriceearning.getId(),tbStoPriceearning.getFieldYear(),
				tbStoPriceearning.getFirstQuarter(),tbStoPriceearning.getMetaphase(),
				tbStoPriceearning.getThirdQuarter(),tbStoPriceearning.getWholeYear()
		);
		return stoPriceearningDTO;
	}

	public StoStockService getStoStockService() {
		return stoStockService;
	}

	public void setStoStockService(StoStockService stoStockService) {
		this.stoStockService = stoStockService;
	}
}
